using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_PNH
    {
        public clsCHI_TIET_PNH(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }

        #region Private methods
        private CHI_TIET_PNH GetDataRow(DataRow row)
        {
            CHI_TIET_PNH objCHI_TIET_PNH = new CHI_TIET_PNH();
            try
            {
                if (row["MaChiTiet"] != DBNull.Value)
                    objCHI_TIET_PNH.MaChiTiet = int.Parse(row["MaChiTiet"].ToString());
            }
            catch { }
            try
            {
                if (row["MaChiTietPDH"] != DBNull.Value)
                    objCHI_TIET_PNH.MaChiTietPDH = int.Parse((row["MaChiTietPDH"]).ToString());
            }
            catch { }
            try
            {
                if (row["MaKho"] != DBNull.Value)
                    objCHI_TIET_PNH.MaKho = (row["MaKho"].ToString());
            }
            catch { }
            try
            {
                if (row["MaPhieuHuyNhapHang"] != DBNull.Value)
                    objCHI_TIET_PNH.MaPhieuHuyNhapHang = (row["MaPhieuHuyNhapHang"].ToString());
            }
            catch { }
            try
            {
                if (row["MaPhieuNhapHang"] != DBNull.Value)
                    objCHI_TIET_PNH.MaPhieuNhapHang = (row["MaPhieuNhapHang"].ToString());
            }
            catch { }

            try
            {
                if (row["TinhTrang"] != DBNull.Value)
                    objCHI_TIET_PNH.TinhTrang = int.Parse(row["TinhTrang"].ToString());
            }
            catch { }

            try
            {
                if (row["SoLuong"] != DBNull.Value)
                    objCHI_TIET_PNH.SoLuong = int.Parse(row["SoLuong"].ToString());
            }
            catch { }
            try
            {
                if (row["MaVTPT"] != DBNull.Value)
                    objCHI_TIET_PNH.MaVTPT = (row["MaVTPT"].ToString());
            }
            catch { }
            try
            {
                if (row["TenVTPT"] != DBNull.Value)
                    objCHI_TIET_PNH.TenVTPT = (row["TenVTPT"].ToString());
            }
            catch { }
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objCHI_TIET_PNH.MaTrangThai = int.Parse(row["MaTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objCHI_TIET_PNH.TenTrangThai = (row["TenTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["MaTrangThai"] != DBNull.Value)
                    objCHI_TIET_PNH.oTRANG_THAI_VTPT.MaTrangThai = int.Parse(row["MaTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["TenTrangThai"] != DBNull.Value)
                    objCHI_TIET_PNH.oTRANG_THAI_VTPT.TenTrangThai = (row["TenTrangThai"].ToString());
            }
            catch { }
            try
            {
                if (row["ItemCategory"] != DBNull.Value)
                    objCHI_TIET_PNH.oTRANG_THAI_VTPT.ItemCategory = (row["ItemCategory"].ToString());
            }
            catch { }
            try
            {
                if (row["ValuationType"] != DBNull.Value)
                    objCHI_TIET_PNH.oTRANG_THAI_VTPT.ValuationType = (row["ValuationType"].ToString());
            }
            catch { }
            try
            {
                if (row["MaKhoThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PNH.oKHO._maKhoThamChieuSAP = (row["MaKhoThamChieuSAP"].ToString());
            }
            catch { }
            try
            {
                if (row["NgayHuyNhapHang"] != DBNull.Value)
                    objCHI_TIET_PNH.NgayHuyNhapHang = Convert.ToDateTime(row["NgayHuyNhapHang"].ToString());
            }
            catch (Exception ex)
            {
            }

            try
            {
                if (row["POItem_No"] != DBNull.Value)
                    objCHI_TIET_PNH.POItem_No = int.Parse(row["POItem_No"].ToString());
            }
            catch { }

            try
            {
                if (row["AccAssignment"] != DBNull.Value)
                    objCHI_TIET_PNH.AccAssignment = (row["AccAssignment"].ToString());
            }
            catch { }

            try
            {
                if (row["MaTaiSan"] != DBNull.Value)
                    objCHI_TIET_PNH.MaTaiSan = (row["MaTaiSan"].ToString());
            }
            catch { }

            try
            {
                if (row["MaPhieuIn"] != DBNull.Value)
                    objCHI_TIET_PNH.MaPhieuIn = int.Parse((row["MaPhieuIn"]).ToString());
            }
            catch { }

            try
            {
                if (row["POThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PNH.POThamChieuSAP = (row["POThamChieuSAP"].ToString());
            }
            catch { }

            try
            {
                if (row["SoLuongPO"] != DBNull.Value)
                    objCHI_TIET_PNH.SoLuongPO = int.Parse(row["SoLuongPO"].ToString());
            }
            catch { }

            try
            {
                if (row["TenKho"] != DBNull.Value)
                    objCHI_TIET_PNH.oKHO.TenKho = (row["TenKho"].ToString());
            }
            catch { }

            try
            {
                if (row["MaNhaSanXuat"] != DBNull.Value)
                    objCHI_TIET_PNH.MaNhaSanXuat = (row["MaNhaSanXuat"].ToString());
            }
            catch { }
            return objCHI_TIET_PNH;
        }
        private List<CHI_TIET_PNH> GetCHI_TIET_PNH(DataTable dt)
        {
            List<CHI_TIET_PNH> lst = new List<CHI_TIET_PNH>();
            CHI_TIET_PNH obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        #region Public methods
        public List<CHI_TIET_PNH> usp_SelectCHI_TIET_PNH_ByPOThamChieuSAP(string POThamChieuSAP)
        {
            string spName = "usp_SelectCHI_TIET_PNH_ByPOThamChieuSAP";
            DataTable dt = new DataTable();
            List<CHI_TIET_PNH> lst = new List<CHI_TIET_PNH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@POThamChieuSAP", POThamChieuSAP);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PNH(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public List<CHI_TIET_PNH> usp_SelectCHI_TIET_PNH_ByMaPhieuNhapHang(string MaPhieuNhapHang)
        {
            string spName = "usp_SelectCHI_TIET_PNH_ByMaPhieuNhapHang";
            DataTable dt = new DataTable();
            List<CHI_TIET_PNH> lst = new List<CHI_TIET_PNH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaPhieuNhapHang", MaPhieuNhapHang);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PNH(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public List<CHI_TIET_PNH> usp_SelectCHI_TIET_PNH_ByMaChiTietPDH(int MaChiTietPDH)
        {
            string spName = "usp_SelectCHI_TIET_PNH_ByMaChiTietPDH";
            DataTable dt = new DataTable();
            List<CHI_TIET_PNH> lst = new List<CHI_TIET_PNH>();
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChiTietPDH", MaChiTietPDH);

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, par);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, par);
                }

                lst = this.GetCHI_TIET_PNH(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public int usp_UpdateCHI_TIET_PNH(CHI_TIET_PNH obj)
        {
            string spName = "usp_UpdateCHI_TIET_PNH";
            int kq = 0;
            try
            {
                SqlHelper.OpenConnection();

                SqlParameter[] par = new SqlParameter[9];

                par[0] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                par[1] = new SqlParameter("@SoLuong", obj.SoLuong);
                par[2] = new SqlParameter("@MaChiTietPDH", obj.MaChiTietPDH);
                par[3] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                par[4] = new SqlParameter("@MaPhieuNhapHang", obj.MaPhieuNhapHang);
                par[5] = new SqlParameter("@MaPhieuHuyNhapHang", obj.MaPhieuHuyNhapHang);
                par[6] = new SqlParameter("@MaKho", obj.MaKho);
                par[7] = new SqlParameter("@NguoiTao", obj.NguoiNhap);
                par[8] = new SqlParameter("@NgayTao", obj.NgayNhap);

                if (!SqlHelper.IsHaveTran())
                   kq = SqlHelper.ExecuteNonQuery(spName, par);
                else
                {
                    kq = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                }

               
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return kq;
        }

        public int usp_UpdateCHI_TIET_PNH_Cancelation(List<CHI_TIET_PNH> lstCT)
        {
            string spName = "usp_UpdateCHI_TIET_PNH_Cancelation";
            int kq = 0;
            try
            {
                CHI_TIET_PNH obj;
                SqlHelper.BeginTran();
                int i=0;
                for ( i = 0; i < lstCT.Count; i++)
                {
                    obj = new CHI_TIET_PNH();
                    obj = lstCT[i];                                   
                    
                    SqlParameter[] par = new SqlParameter[10];
                    par[0] = new SqlParameter("@RETURNVALUE", SqlDbType.Int);
                    par[0].Direction = ParameterDirection.ReturnValue;
                    par[1] = new SqlParameter("@MaChiTiet", obj.MaChiTiet);
                    par[2] = new SqlParameter("@SoLuong", obj.SoLuong);
                    par[3] = new SqlParameter("@MaChiTietPDH", obj.MaChiTietPDH);
                    par[4] = new SqlParameter("@TinhTrang", obj.TinhTrang);
                    par[5] = new SqlParameter("@MaPhieuNhapHang", obj.MaPhieuNhapHang);
                    par[6] = new SqlParameter("@MaPhieuHuyNhapHang", obj.MaPhieuHuyNhapHang);
                    par[7] = new SqlParameter("@MaKho", obj.MaKho);
                    par[8] = new SqlParameter("@NguoiTao", obj.NguoiNhap);
                    par[9] = new SqlParameter("@NgayTao", obj.NgayNhap);

                    if (!SqlHelper.IsHaveTran())
                        SqlHelper.ExecuteNonQuery(spName, par);
                    else
                    {
                        SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                    }
                    kq = int.Parse(par[0].Value.ToString());
                    if (kq <= 0)
                        SqlHelper.Tran.Rollback();
                }

                if (i == lstCT.Count)
                    SqlHelper.Tran.Commit();
                else
                    SqlHelper.Tran.Rollback();

            }
            catch (Exception ex)
            {
                kq = -1;
                //SqlHelper.Tran.Rollback();
            }
            finally
            {
                SqlHelper.CloseConnection();
            }
            return kq;
        }
        #endregion
    }
}
